package com.shopping;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.shopping.exception.PasswordNotCorrectException;
import com.shopping.exception.UserNameNotCorrectException;
import com.shopping.util.DB;

public class User {

	private int id;
	private String username;
	private String password;
	private String addr;
	private String phone;
	private  Timestamp  rdate;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public Timestamp getRdate() {
		return rdate;
	}
	public void setRdate(Timestamp rdate) {
		this.rdate = rdate;
	}
	
	
	
	public void save(){
		
		Connection connect = DB.getConnect();
		String sql = "insert into ruser(id,username,password,phone,addr,rdate) values(null,?,?,?,?,?)";
		PreparedStatement pstmt = DB.getPrepareStatement(connect,sql);
		
		try {
			pstmt.setString(1, username);
			pstmt.setString(2, password);
			pstmt.setString(3, phone);
			pstmt.setString(4, addr);
			pstmt.setTimestamp(5, rdate);
			
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DB.closStatement(pstmt);
			DB.closConn(connect);
		}
		
		
	}
	
	
	
	public static List<User> findAllUsers(){
		
		List<User> list = new ArrayList<User>();
		Connection connect =null;
		ResultSet rs = null;
		try{
			connect = DB.getConnect();
			String sql = "select * from  ruser";
			rs = DB.executeQuery(connect,sql);	
			
			
			while(rs.next()){
				int id = rs.getInt("id");
				String username = rs.getString("username");
				String password = rs.getString("password");
				String addr = rs.getString("addr");
				String phone = rs.getString("phone");
				Timestamp rdate = rs.getTimestamp("rdate");
				User u = new User();
				u.setId(id);
				u.setUsername(username);
				u.setPassword(password);
				u.setAddr(addr);
				u.setPhone(phone);
				u.setRdate(rdate);
				
				list.add(u);
			}
		}catch(SQLException e ){
			e.printStackTrace();
		}finally{
			if(rs!=null){
				DB.closResultSet(rs);
			}
			
			if(connect!=null){
				DB.closConn(connect);
			}
		}
		
		return list;
	}
	
	
	
	public static void deleteUser(int id){
		
		Connection connect = null;
		PreparedStatement pstmt = null;
		try{
			connect = DB.getConnect();
			String sql ="delete from ruser where id = ?";
			pstmt = DB.getPrepareStatement(connect, sql);
			
			pstmt.setInt(1, id);
			pstmt.executeUpdate();
		}catch(SQLException e ){
			e.printStackTrace();
		}finally{
			DB.closStatement(pstmt);
			DB.closConn(connect);
		}
		
	}
	
	/**
	 * 查询某个用户名是否已经存在
	 * @param userName
	 * @return
	 */
	public static boolean findExistUserName(String userName){
		Connection connect = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try{
			connect = DB.getConnect();
			String sql ="select *  from ruser where username = ?";
			pstmt = DB.getPrepareStatement(connect, sql);
			
			pstmt.setString(1, userName);
			rs = pstmt.executeQuery();
			if(rs.next()){//如果存在结果集那么就证明此名字存在
				return true;
			}else{
				return false;
			}
		}catch(SQLException e ){
			e.printStackTrace();
		}finally{
			DB.closResultSet(rs);
			DB.closStatement(pstmt);
			DB.closConn(connect);
		}
		
		return true;
	}
	
	
	
	/**
	 * 查询某个密码是否正确
	 * @param userName
	 * @return
	 */
	public static boolean findPasswordCorrect(String userName,String password){
		
		Connection connect = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		
		try{
			
			connect = DB.getConnect();
			String sql ="select *  from ruser where username = ? and password = ?";
			pstmt = DB.getPrepareStatement(connect, sql);
			
			pstmt.setString(1, userName);
			pstmt.setString(2, password);
			
			rs = pstmt.executeQuery();
			
			if(rs.next()){//如果存在结果集那么就证明此名字存在
				return true;
			}else{
				return false;
			}
			
			
		}catch(SQLException e ){
			e.printStackTrace();
		}finally{
			DB.closResultSet(rs);
			DB.closStatement(pstmt);
			DB.closConn(connect);
		}
		
		return false;
	}
	
	
	
	
	/**
	 * 查询某个用户的信息
	 * @param userName
	 * @return
	 * @throws UserNameNotCorrectException 用户名不存在
	 * @throws PasswordNotCorrectException  密码不正确
	 */
	public static User findUserCorrect(String userName,String password) throws UserNameNotCorrectException, PasswordNotCorrectException{
		
		Connection connect = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		
		try{
			
			connect = DB.getConnect();
			String sql ="select *  from ruser where username = ?";
			pstmt = DB.getPrepareStatement(connect, sql);
			
			pstmt.setString(1, userName);
			
			rs = pstmt.executeQuery();
			
			if(!rs.next()){//如果不存在结果集那么就证明此名字不存在
				throw new UserNameNotCorrectException();
			}else{
				
				String sql1 ="select *  from ruser where username = ? and password = ?";
				pstmt = DB.getPrepareStatement(connect, sql1);
				
				pstmt.setString(1, userName);
				pstmt.setString(2, password);
				ResultSet rs1 = pstmt.executeQuery();
				
				if(!rs1.next()){//如果不存在，说明密码不正确
					throw new PasswordNotCorrectException();
				}else{
					
					while(rs1.next()){
						
						int id = rs1.getInt("id");
						String username = rs1.getString("username");
						String temppwd = rs1.getString("password");
						String phone = rs1.getString("phone");
						String addr = rs1.getString("addr");
						Timestamp rdate = rs1.getTimestamp("rdate");
						
						
						User u = new User();
						u.setId(id);
						u.setUsername(username);
						u.setPassword(temppwd);
						u.setPhone(phone);
						u.setAddr(addr);
						u.setRdate(rdate);
						
						return u;
					}
				}
				
				
			}
			
			
		}catch(SQLException e ){
			e.printStackTrace();
		}finally{
			DB.closResultSet(rs);
			DB.closStatement(pstmt);
			DB.closConn(connect);
		}
		
		return null;
	}
	
	
	
}
